package com.hunau;

public class Test {
	public static void merge(int[] A, int m, int[] B, int n) {
		int indexA = m - 1;
		int indexB = n - 1;
		int indexMerged = m + n - 1;
		while (indexA >= 0 && indexB >= 0) {
			if (A[indexA] > B[indexB]) {
				A[indexMerged] = A[indexA];
				indexMerged--;
				indexA--;
			} else {
				A[indexMerged] = B[indexB];
				indexMerged--;
				indexB--;
			}
		}
		while (indexB >= 0) {
			A[indexMerged] = B[indexB];
			indexMerged--;
			indexB--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int m = 3, n = 3;
		int[] A = new int[6];
		A[0] = 4;
		A[1] = 5;
		A[2] = 6;
		int[] B = { 1, 2, 3 };
		System.out.print("A数组合并排序前：");
		for (int i = 0; i < m + n; i++) {
			System.out.print(A[i] + " ");
		}
		merge(A, 3, B, 3);
		System.out.print("A数组合并排序后：");
		for (int i = 0; i < m + n; i++) {
			System.out.print(A[i] + " ");
		}
	}

}
